(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.glEntryList.listController", listController);
	listController.$inject = [ "$scope", "$uibModalInstance", "dialogsModule", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService" ];
	function listController($scope, $uibModalInstance, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.validatorConfig = validatorConfig;

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.init = function() {
			if (!ctrl.dialogsModule.params.refTxnId) {
				return;
			}
			ctrl.searchCriteria = ctrl.searchCriteria || {};
			ctrl.searchCriteria = angular.merge(ctrl.searchCriteria, ctrl.dialogsModule.params);
			ctrl.search();
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			ctrl.innerGrids = [];
			ctrl.tableTitleConfig = [];

			authService.loanGlEntryList(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				var glEntryList = groupList(ctrl.searchCriteria.list);
				angular.forEach(glEntryList, function(dataList, index, array) {
					var innerGridOptions = gridService.createInnerGridOptions($scope, angular.copy(layoutConfig.formConfigs.glEntryList.tableConfigs.table), function (eventRow, selectedRows) {
						ctrl.selectedRows = selectedRows;
					});
					innerGridOptions.data = "vm.glEntryList[" + index + "]";
					ctrl.innerGrids.push(innerGridOptions);
					ctrl.tableTitleConfig[index] = angular.copy(layoutConfig.formConfigs.tableTitle);
				});
				ctrl.glEntryList = glEntryList;
				ctrl.layoutConfig = layoutConfig;
			}, angular.noop);
		};
		// ctrl.glEntryList.glEntrySubList
		function groupList(glEntryList) {
			var entryList = [];
			if (glEntryList && glEntryList.length > 0) {
				var itemCcy = "", crDr = "";
				for (var i = 0; i < glEntryList.length; i++) {// add item for
																// sub list
					// For formatting
					glEntryList[i].itemCcy_cache = glEntryList[i].itemCcy;
					var glList = existList(glEntryList[i], entryList);
					if (glList) {
						if (itemCcy == glEntryList[i].itemCcy && crDr == glEntryList[i].crDr) {
							glEntryList[i].itemCcy = "";
							glEntryList[i].crDr = "";
						} else if (itemCcy == glEntryList[i].itemCcy) {
							glEntryList[i].itemCcy = "";
							crDr = glEntryList[i].crDr;
						} else {
							itemCcy = glEntryList[i].itemCcy;
							crDr = glEntryList[i].crDr;
						}
						glList.push(glEntryList[i]);
					} else {// add sub list
						var glEntrySubList = [];
						glEntrySubList.push(glEntryList[i]);
						entryList.push(glEntrySubList);
						itemCcy = glEntryList[i].itemCcy;
						crDr = glEntryList[i].crDr;
					}
				}
			}
			return entryList;
		}

		function existList(item, list) {
			var flag = null;
			if (list && list.length > 0) {
				angular.forEach(list, function(dataList, index, array) {
					if (dataList[0].txnId == item.txnId && dataList[0].txnType == item.txnType && angular.equals(dataList[0].effectiveDate, item.effectiveDate)) {
						flag = dataList;
					}
				});
			}
			return flag;
		}
	}
})(angular);